//
//  XNLoadBudgetViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/14.
//

#import "XNLoadBudgetViewController.h"
#import "XNLoadBudgetBillInfoModel.h"
#import "XNLoadBudgetBillInfoRequest.h"

@interface XNLoadBudgetViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNLoadBudgetBillInfoModel *infoModel;

@end

@implementation XNLoadBudgetViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNLoadBudgetBillInfoRequest *infoRequest = [[XNLoadBudgetBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNLoadBudgetBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        weakSelf.approveArray = request.infoModel.historyList;
        dispatch_group_leave(group);
    } failure:^(__kindof XNLoadBudgetBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
//    dispatch_group_enter(group);
//    XNCareConfirmApproveRequest *approveRequest = [[XNCareConfirmApproveRequest alloc] initWithOrderId:self.orderId];
//    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNCareConfirmApproveRequest * _Nonnull request) {
//        weakSelf.approveArray = request.approveFlowArray;
//        dispatch_group_leave(group);
//    } failure:^(__kindof XNCareConfirmApproveRequest * _Nonnull request) {
//        dispatch_group_leave(group);
//    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}

- (void)creatTableData
{
    
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray;
    if ([self.infoModel.ID hasPrefix:@"BC"]) {//公司年度
        showItemArray = [NSArray arrayWithObjects:
                                  [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                                  [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                                  [NSString stringWithFormat:@"制单人： %@",self.infoModel.userName],
                         [NSString stringWithFormat:@"职务： %@%@",self.infoModel.userDepartmentName.length > 0 ? self.infoModel.userDepartmentName : @"",
                          self.infoModel.positionName.length > 0 ? self.infoModel.positionName : @""],
                                  nil];
    }else{//部门年度
        showItemArray = [NSArray arrayWithObjects:
                                  [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                                  [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                                  [NSString stringWithFormat:@"制单人： %@",self.infoModel.userName],
                                  [NSString stringWithFormat:@"职务： %@",self.infoModel.userDepartmentName],
                                  nil];
    }

    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"申请信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2;
    if ([self.infoModel.ID hasPrefix:@"BC"]) {//公司年度
        showItemArray2 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"公司: %@",self.infoModel.companyName],
                                   [NSString stringWithFormat:@"预算年份： %ld",self.infoModel.year],
                                   [NSString stringWithFormat:@"版本: %@",self.infoModel.version],
                                   [NSString stringWithFormat:@"备注： %@",@""],
                                   nil];
    }else{//部门年度
        showItemArray2 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"公司: %@",self.infoModel.companyName],
                                   [NSString stringWithFormat:@"部门: %@",self.infoModel.departmentName],
                                   [NSString stringWithFormat:@"预算年份： %ld",self.infoModel.year],
                                   [NSString stringWithFormat:@"版本: %@",self.infoModel.version],
                                   [NSString stringWithFormat:@"备注： %@",@""],
                                   nil];
    }

    itemModel2.showItemArray = showItemArray2;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    
    XNTableShowDataModel *showModel4 = [[XNTableShowDataModel alloc] init];
    showModel4.showType = XNShowType_MoreData;
    showModel4.title = @"子表信息";
    showModel4.showDataArray = [self creatSubTableData];
    [self.showArray addObject:showModel4];
    
    
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}

- (NSMutableArray *)creatSubTableData
{
    __block NSMutableArray *array = [NSMutableArray array];
    
    NSArray *strArray = [NSArray arrayWithObjects:@"收入",@"营业税金及附加",@"成本",@"费用",@"其他损失及收益", nil];
    [self.infoModel.budgetTypeList enumerateObjectsUsingBlock:^(XNBudgetTypeModel * _Nonnull typeModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        XNTableShowDataModel *showData = [[XNTableShowDataModel alloc] init];
        showData.title = strArray[idx];
        [typeModel.budgetNoDetailList enumerateObjectsUsingBlock:^(XNbudgetNoDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            XNShowItemModel *item = [[XNShowItemModel alloc] init];
            item.showItemArray = [NSArray arrayWithObjects:
                                  [NSString stringWithFormat:@"预算项目：%@", obj.budgetTypeName],
                                  [NSString stringWithFormat:@"预算项说明：%@",obj.budgetTypeRemark.length > 0 ? obj.budgetTypeRemark : @""],
                                  [NSString stringWithFormat:@"1月：%.3f",obj.january],
                                  [NSString stringWithFormat:@"2月：%.3f",obj.february],
                                  [NSString stringWithFormat:@"3月：%.3f",obj.march],
                                  [NSString stringWithFormat:@"4月：%.3f",obj.april],
                                  [NSString stringWithFormat:@"5月：%.3f",obj.may],
                                  [NSString stringWithFormat:@"6月：%.3f",obj.june],
                                  [NSString stringWithFormat:@"7月：%.3f",obj.july],
                                  [NSString stringWithFormat:@"8月：%.3f",obj.august],
                                  [NSString stringWithFormat:@"9月：%.3f",obj.september],
                                  [NSString stringWithFormat:@"10月：%.3f",obj.october],
                                  [NSString stringWithFormat:@"11月：%.3f",obj.november],
                                  [NSString stringWithFormat:@"12月：%.3f",obj.december],
                                  [NSString stringWithFormat:@"汇总：%.3f",obj.totalBudget],
                                  [NSString stringWithFormat:@"预算说明：%@",obj.remark.length > 0 ? obj.remark :@""],
                                  nil];
            [showData.showDataArray addObject:item];
        }];
        [array addObject:showData];
    }];

    

    
    return array;
}


@end
